﻿using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CrazySname.GameClasses
{
    public class Alimento : IDesenhavel
    {
        public Texture2D Textura { get; set; }
        public Point Posicao { get; set; }
        public Vector2 AreaDeAtuacao { get; private set; }
        public Vector2 Inicio { get; private set; }

        public Alimento(float x, float y, Texture2D textura)
        {
            AreaDeAtuacao = new Vector2(x - 1, y - 1);
            Textura = textura;
        }

        public void RandomizaAlimento(Cobra cobra, Utilitarios gameUtilitarios)
        {
            Inicio = cobra.Inicio;
            var random = new Random();
            do
            {
                var X = random.Next((int)AreaDeAtuacao.X);
                var Y = random.Next((int)AreaDeAtuacao.Y);
                var boolean = random.Next(1);
                X = boolean == 1 ? X : X*-1;
                boolean = random.Next(1);
                Y = boolean == 1 ? Y : Y * -1;
                Posicao = new Point(X, Y);
            } while (cobra.Partes.Any(parte => parte.Posicao == Posicao));
        }

        public void Desenhar(SpriteBatch spriteBatch)
        {
            var posicao = Inicio + new Vector2(Posicao.X * Utilitarios.Const, Posicao.Y * Utilitarios.Const);
            spriteBatch.Draw(Textura, posicao, Color.White);
        }
    }
}
